package com.ad.pet.app.type;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import com.ad.pet.Logger;
import com.ad.pet.MainActivityCompent;
import com.ad.pet.R;
import com.ad.pet.app.RxDisposable;
import com.ad.pet.common.data.utils.SpUtil;
import com.ad.pet.common.structure.events.RxBus;
import com.ad.pet.common.structure.mvp.BasePresenter;
import com.ad.pet.common.structure.ui.fragment.LoadingFragment;
import com.ad.pet.constant.Constant;
import com.ad.pet.utils.AndroidInterface;
import com.just.agentweb.AgentWeb;

import javax.inject.Inject;

import butterknife.BindView;
import rx.functions.Action1;

/**分类的部分，这里之前是非原生做的，所以比较卡顿，现在改为原生了流畅的很
 * 常见的分类大法，可好用了
 * Created by SEELE on 2018/7/31
 * 苏FT62B0.
 */

public class TypeFragment extends LoadingFragment {
    AgentWeb mAgentWeb;
    @BindView(R.id.mContainer)
    LinearLayout mContainer;
    @Inject
    RxDisposable mRxDisposable;

    @Override
    protected void initData() {
        mStatusLayoutManager.showContent();
    }

    @Override
    protected void initEvents() {

        mRxDisposable.add(RxBus.getDefault().toObservable(String.class)
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String s) {
                        if ("set_message_number_success".equals(s)) {
                            setWebview(getString(R.string.ip_default)+"/index.php/wap/index?token="+ SpUtil.getInstance().getString(Constant.ACCESS_TOKEN));
                        }
                    }
                }));

    }

    @Override
    protected void initViews() {
        //    Navigation.goCommonWebView(getActivity(), "", "https://test-adang.meshsh.com/index.php/wap/index?token="+SpUtil.getInstance().getString(Constant.ACCESS_TOKEN), false, false, MainActivity.REQUEST_GO_MALL);
        setWebview(getString(R.string.ip_default)+"/index.php/wap/index?token="+ SpUtil.getInstance().getString(Constant.ACCESS_TOKEN));
    }

    @Override
    public void initInjector() {
        getComponent(MainActivityCompent.class).inject(this);
    }


    @Override
    protected int contentLayout() {
        return  R.layout.fragment_ali_type;
    }

    @Override
    protected void clickRetry(View v) {
    }

    @Override
    protected BasePresenter getPresenter() {
        return null;
    }

    public static TypeFragment newInstance() {
        Bundle args = new Bundle();
        TypeFragment fragment = new TypeFragment();
        fragment.setArguments(args);
        return fragment;
    }



    private void setWebview(String url) {
        mAgentWeb = AgentWeb.with(getActivity())
                .setAgentWebParent(mContainer, new LinearLayout.LayoutParams(-1, -1)).useDefaultIndicator(getResources().getColor(R.color.colorAccent), 1)
                .createAgentWeb()
                .ready()
                .go(url);
        Logger.e(url);
        mAgentWeb.getJsInterfaceHolder().addJavaObject("android", new AndroidInterface(mAgentWeb, getActivity()));
        mAgentWeb.getAgentWebSettings().getWebSettings().setDomStorageEnabled(true);

//        StringBuffer buffer = new StringBuffer();
//        buffer.append("token=").append(getAccessToken());
//        Logger.e(buffer.toString());
//        mAgentWeb.getWebCreator().getWebView().postUrl(url, buffer.toString().getBytes());
    }

}
